package com.sys.controller;

import com.sys.common.constant.SSEMessageConstants;
import com.sys.common.sse.SseComponent;
import com.sys.system.service.INoticeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Objects;

/**
 * @author LGY
 * Create by 2025/6/26 8:13
 */
@RestController
@RequestMapping("/sse")
public class SseController {

    @Autowired
    private SseComponent wsComponent;
    @Autowired
    private INoticeUserService noticeUserService;

    /**
     * 客户端连接 SSE 的接口
     * 参数 clientId 用于标识不同的客户端
     */
    @GetMapping(value = "/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter connect(@RequestParam String clientId, @RequestParam String type, @RequestParam String custom) {
        SseEmitter sseEmitter = wsComponent.registerClient(clientId, type, custom);
        if (Objects.equals(type, SSEMessageConstants.SERVICE_TYPE.NOTICE)) {
            noticeUserService.sendClientMessage(clientId, type, custom);
        }
        return sseEmitter;
    }

}
